const { Controller } = require('egg');

class GoodsController extends Controller {
  // 增加
  async create() {
    try {
      let goodsInfo = this.ctx.request.body;
      // console.log(goodsInfo);
      let rel = await this.ctx.service.goods.addGoods(goodsInfo);
      console.log(rel);
      this.ctx.body = {
        code: 20000,
        message: '增加商品成功'
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }

  }
  // 获取
  async index() {
    try {
      let query = this.ctx.request.query;
      let goodsData = await this.ctx.service.goods.getGoods(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  //  删除
  async destroy() {
    try {
      let id = this.ctx.params.id;
      let rel = await this.ctx.service.goods.deleteGoods(id);
      console.log(rel);
      this.ctx.body = {
        code: 20000,
        message: '删除商品成功'
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  //  获取一个商品
  async show() {
    try {
      let id = this.ctx.params.id;
      let goodsData = await this.ctx.service.goods.getOne(id);
      this.ctx.body = {
        code: 20000,
        message: '获取一个成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 修改商品
  async update() {
    try {
      let id = this.ctx.params.id;
      let newInfo = this.ctx.request.body;
      let rel = await this.ctx.service.goods.changeGoods(id, newInfo);
      console.log(rel);
      this.ctx.body = {
        code: 20000,
        message: '修改商品成功'
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 搜索商品
  async searchGoodsList() {
    let query = this.ctx.request.query;
    let rel = await this.ctx.service.goods.searchGoods(query);
    this.ctx.body = {
      code: 20000,
      message: '获取数据成功',
      data: rel
    };
  }





  // 以下全是前台数据请求******************
  // 前台获取最新商品
  async getNewGoodsData() {
    try {
      // 固定获取最新的6件商品数据
      let query = {
        currentPage: 1,
        pageSize: 6
      };
      let goodsData = await this.ctx.service.wxpage.getNewGoodsList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 获取销量最好的商品(优选精品)
  async getSellGoodsData() {
    try {
      // 固定获取4件商品数据
      let query = {
        currentPage: 1,
        pageSize: 4
      };
      let goodsData = await this.ctx.service.wxpage.getSellGoodsList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 获取综合商品列表
  async getGoodsAllData(){
    try {
      let query = this.ctx.request.query;
      let goodsData = await this.ctx.service.wxpage.getGoodsAllList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 按销量排行商品列表
  async getGoodsSalesData() {
    try {
      let query = this.ctx.request.query;
      let goodsData = await this.ctx.service.wxpage.getGoodsSalesList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }

  // 新品
  async getGoodsNewData() {
    try {
      let query = this.ctx.request.query;
      let goodsData = await this.ctx.service.wxpage.getGoodsNewList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }
  // 价格
  async getGoodsPriceData() {
    try {
      let query = this.ctx.request.query;
      let goodsData = await this.ctx.service.wxpage.getGoodsPriceList(query);
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: goodsData
      };
    } catch (error) {
      this.ctx.body = {
        code: 50000,
        message: '服务器错误'
      };
    }
  }

  // 推荐随机4件商品
  async getRecommendData() {
    try {
      let recommendData = await this.ctx.service.wxpage.getRecommendGoodsList();
      this.ctx.body = {
        code: 20000,
        message: '获取商品成功',
        data: recommendData
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        msg: '推荐失败'
      };
    }
  }
  // 单个商品信息
  async getOneGoodsData() {
    try {
      let id = this.ctx.params.id;
      let goodsData = await this.ctx.service.wxpage.getOneGoodsInfo(id);
      this.ctx.body = {
        code: 20000,
        message: '获取商品信息成功',
        data: goodsData
      };
    } catch (err) {
      this.ctx.body = {
        code: 50000,
        msg: '获取失败'
      };
    }
  }

}

module.exports = GoodsController;
